package kuanjia.EF2Select;


public class demo1 {
    /**
     * 寻找一个数（基本的二分搜索)
     * @param nums
     * @param target
     * @return
     */
    public int binarySearch(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1; // 注意

        //while(left <= right) 的终止条件是 left == right + 1
//        while(left < right) 的终止条件是 left == right
        while (left<=right){
            int mid = left+(right-left)/2;
            if (nums[mid]==target)
                return mid;
            else if (nums[mid]<target)
                left = mid+1;
            else if (nums[mid]>target)
                right = mid-1;
        }
        return -1;
    }
}
